<template>
  <div class="table_container">
    <div class="search_box">
      <el-row :gutter="20">
        <el-col :md="8">
          <el-input
            v-model="searchInpt"
            placeholder="输入设备码/ID/手机号"
            clearable
            @keyup.enter.native="searchEquipmentManageData()"
          >
            <el-button
              @click="searchEquipmentManageData()"
              icon="el-icon-search"
              slot="append"
              :loading="searchLoading"
            >搜索</el-button>
          </el-input>
        </el-col>
        <el-col :md="4">
        </el-col>
        <el-col :md="6">
          <el-select
            v-model="bindingTimeValue"
            placeholder="请选择"
            @change="changeBindingTimeValue()"
          >
            <el-option
              v-for="item in bindingTimeSort"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :md="6">
        </el-col>
      </el-row>
    </div>

    <el-table
      v-loading="tableloading"
      :data="equipmentData"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        prop="hardwareEquipment"
        label="设备编号"
        align="center"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="userId"
        label="用户ID"
        align="center"
      ></el-table-column>
      <!-- <el-table-column prop="nickName" label="昵称" align="center"></el-table-column> -->
      <!-- <el-table-column prop="username" label="手机号" align="center"></el-table-column> -->
      <el-table-column
        prop="bandingCount"
        label="绑定次数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="bandingTime"
        :formatter="formatterTime"
        label="最早绑定时间"
        align="center"
        width="110"
      ></el-table-column>
      <el-table-column
        prop="lastBandingTime"
        :formatter="lastFormatterTime"
        label="最后绑定时间"
        align="center"
        width="110"
      ></el-table-column>
      <el-table-column
        prop=""
        label="绑定历史"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="goHistory(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop=""
        label="操作"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="doEquipmentUnbindData(scope.row)"
          >解绑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div
      class="Pagination"
      style="text-align: left;margin-top: 10px;"
    >
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="limit"
        layout="total, prev, pager, next"
        :total="count"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  fetchGetEquipmentManageData,
  fetchEquipmentUnbindData,
  fetchGetEquipmentSortData
} from "@/api/getData";
import { formatDateTime } from "@/util/common";
export default {
  data() {
    return {
      count: 0,
      limit: 20,
      currentPage: 1,
      tableloading: false,
      searchInpt: "",
      searchLoading: false,
      equipmentData: [],
      bindingTimeSort: [
        {
          value: "1",
          label: "按绑定次数排序"
        },
        {
          value: "2",
          label: "按绑定时间排序"
        }
      ],
      bindingTimeValue: "1"
    };
  },
  mounted() {
    this.getEquipmentManageData();
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      if (this.searchInpt !== "") {
        const page = "1";
        this.searchEquipmentManageData(page);
      } else {
        this.getEquipmentManageData();
      }
    },
    async getEquipmentManageData() {
      this.tableloading = true;
      const manage = await fetchGetEquipmentManageData({
        pageSize: this.limit,
        pageNo: this.currentPage,
        searchKey: this.searchInpt,
        sortKey: this.bindingTimeValue
      });
      this.tableloading = false;
      this.equipmentData = [];
      this.count = manage.data.total;
      if (manage.data.list) {
        manage.data.list.forEach(item => {
          const manageTable = {};
          manageTable.bandingCount = item.bandingCount;
          manageTable.bandingTime = item.bandingTime;
          manageTable.lastBandingTime = item.lastBandingTime;
          manageTable.hardwareEquipment = item.hardwareEquipment;
          manageTable.nickName = item.nickName;
          manageTable.userId = item.userId;
          manageTable.username = item.username;
          this.equipmentData.push(manageTable);
        });
      }
    },
    async searchEquipmentManageData(page) {
      if (page !== "1") {
        this.currentPage = 1;
      }
      this.searchLoading = true;
      this.tableloading = true;
      const manage = await fetchGetEquipmentManageData({
        pageSize: this.limit,
        pageNo: this.currentPage,
        searchKey: this.searchInpt,
        sortKey: this.bindingTimeValue
      });
      this.searchLoading = false;
      this.tableloading = false;
      this.equipmentData = [];
      this.count = manage.data.total;
      if (manage.data.list) {
        manage.data.list.forEach(item => {
          const manageTable = {};
          manageTable.bandingCount = item.bandingCount;
          manageTable.bandingTime = item.bandingTime;
          manageTable.lastBandingTime = item.lastBandingTime;
          manageTable.hardwareEquipment = item.hardwareEquipment;
          manageTable.nickName = item.nickName;
          manageTable.userId = item.userId;
          manageTable.username = item.username;
          this.equipmentData.push(manageTable);
        });
      }
    },
    doEquipmentUnbindData(data) {
      this.$confirm("确认解除绑定", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.equipmentUnbindData(data);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    async equipmentUnbindData(data) {
      const unbind = await fetchEquipmentUnbindData({
        hardwareEquipment: data.hardwareEquipment
      });
      if (unbind.status == "200") {
        this.getEquipmentManageData();
        this.$message({
          type: "success",
          message: "删除成功"
        });
      } else {
        this.$message({
          type: "error",
          message: unbind.msg
        });
      }
    },
    changeBindingTimeValue() {
      this.getEquipmentManageData();
    },
    goHistory(data) {
      this.$router.push({
        path: "equipmentHistorybind",
        query: { hardwareEquipment: data.hardwareEquipment }
      });
    },
    formatterTime(row) {
      if (row.bandingTime) {
        return formatDateTime(row.bandingTime);
      } else {
        return "";
      }
    },
    lastFormatterTime(row) {
      if (row.lastBandingTime) {
        return formatDateTime(row.lastBandingTime);
      } else {
        return "";
      }
    }
  }
};
</script>
<style lang="less" scoped>
.search_box {
  padding: 20px 0;
}
</style>

